<template>
  <div>
    <el-form
      ref="lmform"
      label-width="100px"
      :model="form"
      :rules="rules"
      status-icon
    >
      <el-form-item label="资源名" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入资源名"
        ></el-input>
      </el-form-item>
      <el-form-item label="前端地址">
        <el-input
          v-model="form.frontUrl"
          placeholder="请输入前端地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="后端地址">
        <el-input
          v-model="form.backUrl"
          placeholder="请输入后端地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="含有下级">
        <el-radio-group v-model="form.haschildren">
          <el-radio-button :label="1">是</el-radio-button>
          <el-radio-button :label="0">否</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="资源类别">
        <el-radio-group v-model="form.type">
          <el-radio-button :label="1">目录</el-radio-button>
          <el-radio-button :label="0">按钮</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图标">
        <el-input
          v-model="form.icon"
          placeholder="请输入图标"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="save()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "ResourceEdit",
  props: {
      dialogshow: {
          type: Boolean
      },
      editId: {
        type: Number,
        default: null
      },
      parentId: {
        type: Number,
        default: null
      }
  },
  data() {
    return {
      form: {
        name: "",
        frontUrl: '',
        backUrl: '',
        haschildren: 0,
        type: 0,
        icon: ''
      },
      rules: {
        name: [
          { required: true, message: "资源名不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "资源名长度小于20", trigger: "blur" },
        ]
      },
      url: '/ums-resource/add'
    };
  },
  created() {
    if(this.editId) {
      this.url = '/ums-resource/update'
      this.getById()
    } else {
      this.form.parentId = this.parentId
    }
  },
  methods: {
    save() {
      this.$axios.post(this.url, this.form, () => {
        this.$emit('update:dialogshow',false)
        this.$emit('getTable')
      },this.$refs['lmform'])
    },
    getById() {
      this.$axios.get('/ums-resource/getone',{id: this.editId}, response => {
        for(let key in this.form) {
          this.form[key] = response.obj[key]
        }
        this.form.id = response.obj.id
      })
    }
  },
};
</script>
<style lang="less" scoped>
</style>